package com.kuaimacode.kframework.admin.controller.common;

import com.kuaimacode.kframework.admin.services.common.UploadService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.exception.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.api.controller.common
 * @Description: created by IntelliJ IDEA
 * @date 2019-03-16 21:29
 */
@Controller
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @PostMapping("/uploadImg")
    @ResponseBody
    public JsonResult uploadImg(@RequestParam("file") MultipartFile file) {
        try {
            return JsonResult.success(uploadService.uploadFile(file));
        } catch (IOException e) {
            return JsonResult.failure(ErrorEnum.SYSTEM_ERROR.getCode(), ErrorEnum.SYSTEM_ERROR.getMsg());
        }
    }
}
